/**
 * Created by lx on 2017/7/6.
 */
angular.module('service.charge', [
        "ngResource",
    ])
    .service('ChargeService', ChargeService);
ChargeService.$inject = ['$resource', '$filter'];
function ChargeService($resource, $filter) {

    // 通过shopId查询某月的日报表
    this.getDailyMonth = function (shopid, starttime, endtime, success, error) {
        var resource;
        var date = $filter('date')(new Date(), 'yyyy/MM');
        if (date + '/01' == starttime) {
            resource = $resource(electricity_url + "chargebusinesscontroller/getdailymonthbyshopid/:shopid");
        } else {
            resource = $resource(electricity_url + "chargebusinesscontroller/getdailymonthbyshopid/:shopid", {}, {
                query: {
                    method: 'GET',
                    isArray: true,
                    cache: true,
                }
            });
        }
        return resource.query({shopid: shopid, starttime: starttime, endtime: endtime}, success, error);
    };

    // 通过shopId查询某月月报表记录
    this.getMonthly = function (settleDate, shopid, success, error) {
        var resource = $resource(electricity_url + "chargebusinesscontroller/getMonthlyReportRecord?");
        return resource.get({settleDate: settleDate, shopid: shopid}, success, error);
    };

    // 根据店铺id和时间查询交易流水
    this.getCash = function (shopid, starttime, endtime, tradeState, success, error) {
        var resource = $resource(electricity_url + "chargebusinesscontroller/getcashmainbyshopidandtime/:shopid");
        return resource.query({
            shopid: shopid,
            starttime: starttime,
            endtime: endtime,
            tradeState: tradeState
        }, success, error)
    };

    // 获取订单时间区间
    this.getMyOrderList = function (AnalysisService, list, staTime, endtime) {
        var abscissa = []; // 横坐标
        var ordinate = []; // 纵坐标
        var myformat = []; // 截取时间
        var addtime = (endtime - staTime) / (60 * 60 * 24 * 1000);
        for (var i = 0; i < Math.ceil(addtime); i++) {
            var item = {};
            item["start"] = staTime + i * (60 * 60 * 24 * 1000);
            item["end"] = endtime - (60 * 60 * 24 * 1000) * (Math.ceil(addtime) - i - 1);
            myformat.push(item); // 获取坐标用的
            var fees = 0;
            for (var j = 0; j < list.length; j++) {
                if (myformat[i].start < list[j].createTime && myformat[i].end >= list[j].createTime) {
                    fees = fees + 1;
                }
            }

            ordinate.push(fees);
            abscissa.push(formatDate(endtime - (Math.ceil(addtime) - i - 1) * (60 * 60 * 24 * 1000)));//显示日期用的
        }
        var option = AnalysisService.optionData(abscissa, ordinate, 0);
        var diagram = echarts.init(document.getElementById('echant_one'));
        diagram.setOption(option);
    };

    //通过modelid查询套餐详情
    this.getMeal = function(modelid,success,error){
        var resource = $resource(electricity_url + "chargemodecontroller/get/:modelid",{},{
           get:{
               cache:true,
               isArry:true
           }
        });
        return resource.get({modelid: modelid}, success, error);
    };

    // 获取营业额时间区间
    this.getMyListTime = function (AnalysisService, list, staTime, endtime) {
        var abscissa = []; // 横坐标
        var ordinate = []; // 纵坐标
        var myformat = []; // 截取时间
        var addtime = (endtime - staTime) / (60 * 60 * 24 * 1000);
        for (var i = 0; i < Math.ceil(addtime); i++) {
            var item = {};
            item["start"] = staTime + i * (60 * 60 * 24 * 1000);
            item["end"] = endtime - (60 * 60 * 24 * 1000) * (Math.ceil(addtime) - i - 1);
            myformat.push(item); // 获取坐标用的
            var fees = 0;
            for (var j = 0; j < list.length; j++) {
                if (myformat[i].start < list[j].createTime && myformat[i].end >= list[j].createTime) {
                    fees = fees + list[j].payment
                }
            }
            ordinate.push(fees.toFixed(2));
            abscissa.push(formatDate(endtime - (Math.ceil(addtime) - i - 1) * (60 * 60 * 24 * 1000))); // 显示日期用的
        }
        var option = AnalysisService.optionData(abscissa, ordinate, 1);
        var diagram = echarts.init(document.getElementById('echant'));
        diagram.setOption(option);
    };

    this.optionData = function (datetime, alllist, tips) {
        var myname = [];
        if(tips == 0){
            myname = ['当日订单'];
        }else {
            myname = ['当日营业额'];
        }
        return {
            title: {
                text: ''
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: myname,
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                splitLine: {
                    show: false,
                    axisTick: {
                        show: false
                    },
                    lineStyle: {       // 属性lineStyle（详见lineStyle）控制线条样式
                        color: ['#ccc'],
                        width: 1,
                        type: 'dotted',
                    },

                }, // 去除网格线
                type: 'category',
                boundaryGap: false,
                data: datetime,
            },
            yAxis: {
                type: 'value',
                axisTick: {
                    show: false
                },
                axisLine: {
                    show: false,
                }
            },
            series: [{
                name: myname,
                type: 'line',
                stack: '总量',
                itemStyle: {
                    normal: {
                        color: '#4680ee',
                        lineStyle: {
                            color: '#4680ee',
                        }
                    }
                },
                data: alllist,
            }]
        };
    }
}
